/*
 * This file is part of ksPlatform
 * 
 * Copyright (c) 2014-2015
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at 97041437@qq.com.
 */
package cn.com.ksplatform.modules.system.auth.cachemanager.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.ksplatform.base.BaseController;
import cn.com.ksplatform.modules.system.auth.buttonmanager.model.PlatButton;
import cn.com.ksplatform.modules.system.auth.buttonmanager.service.PlatButtonGroupService;
import cn.com.ksplatform.modules.system.auth.cachemanager.util.CacheUtil;
import cn.com.ksplatform.modules.system.auth.loginmanager.controller.LoginCons;
import cn.com.ksplatform.modules.system.auth.menumanager.model.PlatMenu;
import cn.com.ksplatform.modules.system.auth.resourcemanager.model.PlatResource;
import cn.com.ksplatform.modules.system.auth.rolemanager.model.PlatRole;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

/**
 * ClassName:SystemCacheController.java <br/>
 * Date: 2015年12月8日 <br/>
 * 
 * @author Mr.Xiao
 * @version 1.0
 */
@Controller
@RequestMapping("/modules/system/cache")
public class SystemCacheController extends BaseController{
	
	@Autowired
	@Qualifier("cacheManagerFactory")
	CacheManager cacheManager;
	
	@Autowired
	@Qualifier("system.modules.system.PlatButtonGroupService")
	PlatButtonGroupService platButtonGroupService;
	
	Cache cache;
	
	void initCache(){
		if(cache == null){
			cache = cacheManager.getCache("RoleDataCache");
		}
	}
	
	@RequestMapping("getButtons")
	@ResponseBody
	public List<PlatButton> buttons(HttpSession session,String btnGId) throws Exception {
		initCache();
		Set<PlatRole> roleset = (Set<PlatRole>) session.getAttribute(LoginCons.LOGIN_ROLES);
		Set<PlatButton> buttons = new HashSet<PlatButton>();
		List<PlatButton> rtn = new ArrayList<PlatButton>();
		for(PlatRole r : roleset){
			Element em = cache.get(r.getRoleId()+"[btns]");
			if(em != null){
				Set<String> btnIds = (Set<String>) em.getObjectValue();
				List<PlatButton> buttonlist = platButtonGroupService.getButtons(btnGId);
				for(PlatButton b : buttonlist){
					if(btnIds.contains(b.getBtnId())){
						if(rtn.contains(b)){
							continue;
						}
						rtn.add(b);
					}
				}
			}
		}
		Collections.sort(rtn);
		return rtn;
	}
	
	@RequestMapping("getMenus")
	@ResponseBody
	public List<PlatMenu> menus(HttpSession session) throws Exception {
		initCache();
		Set<PlatRole> roleset = (Set<PlatRole>) session.getAttribute(LoginCons.LOGIN_ROLES);
		List<PlatMenu> menus = new ArrayList<PlatMenu>();
		for(PlatRole r : roleset){
			Element em = cache.get(r.getRoleId()+"[menus]");
			if(em != null){
				Set<String> menuIds = (Set<String>) em.getObjectValue();
				for(String menuId : menuIds){
					PlatMenu menu = CacheUtil.getObject("MenuCache", menuId, PlatMenu.class);
					PlatMenu parent = menu.getParent_menu();
					if(parent != null){
						menu.parent_id = parent.getMenuId();
					}
					if(!menus.contains(menu)){
						menus.add(menu);
					}
				}
			}
		}
		Collections.sort(menus);
		return menus;
	}
	
	@RequestMapping("getRes")
	@ResponseBody
	public Set<PlatResource> resources(HttpSession session) throws Exception {
		initCache();
		Set<PlatRole> roleset = (Set<PlatRole>) session.getAttribute(LoginCons.LOGIN_ROLES);
		Set<PlatResource> resources = new HashSet<PlatResource>();
		for(PlatRole r : roleset){
			Element em = cache.get(r.getRoleId()+"[res]");
			if(em != null){
				Set<String> resIds = (Set<String>) em.getObjectValue();
				for(String resId : resIds){
					PlatResource res = CacheUtil.getObject("ResourceCache", resId, PlatResource.class);
					resources.add(res);
				}
			}
		}
		return resources;
	}
	
}
